import 'dart:ui';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:lib_bt/BleManager.dart';
import 'package:lib_bt/send/BleSendManager.dart';
import 'package:lib_util/AssetsLoadUtil.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:lib_util/SPUtils.dart';
import 'code/ble/protocol/AutoConnectUtil.dart';
import 'code/ble/protocol/TouchLink.dart';
import 'code/ble/protocol/iledcolor/ColorBleSupportImpl.dart';
import 'code/ble/protocol/iledcolor/ota/OtaBleSupportImpl.dart';
import 'code/ble/protocol/iledcolor/ota/OtaDialog.dart';
import 'code/ui/home/IndexPage.dart';
import 'generated/l10n.dart';

Future<void> main() async {
  // makes sure plugins are initialized 确保Flutter应用程序的绑定已经初始化完成
  WidgetsFlutterBinding.ensureInitialized();
  //await RustLib.init();
  await SPUtils.init();
  await AssetsLoadUtil.init();

  OtaDialog.instance;
  BleManager.instance.addCheckBleSupport(OtaBleSupportImpl());
  //color相关的初始化
  BleManager.instance.addCheckBleSupport(ColorBleSupportImpl());
  //miniled相关的初始化
  //BleManager.instance.addCheckBleSupport(MiniLedBleSupportImpl());
  //BleManager.instance.addCheckBleSupport(DemoBleSupportImpl());
  //蓝牙通讯相关初始化
  BleSendManager.instance;
  //自动连接
  AutoConnectUtil.instance.autoConnect();

  String langCode = SPUtils.getLangCode("");
  var local;
  if (langCode.isEmpty) {
    local = window.locale;
  } else {
    if (langCode.contains("-")) {
      var list = langCode.split("-");
      if (list.length == 3) {
        local = Locale.fromSubtags(languageCode: list[0], scriptCode: list[1], countryCode: list[2]);
      } else {
        local = Locale.fromSubtags(languageCode: list[0]);
      }
    } else {
      local = Locale.fromSubtags(languageCode: langCode);
    }
  }
  runApp(MyApp(locale: local));
}

class MyApp extends StatelessWidget {
  dynamic locale;

  MyApp({super.key, this.locale});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      builder: BotToastInit(),
      //1. call BotToastInit
      navigatorObservers: [BotToastNavigatorObserver()],
      debugShowCheckedModeBanner: false,
      theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.blueGrey)),
      home: const IndexPage(),
      localizationsDelegates: const [
        AppLocalizationDelegate(),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const AppLocalizationDelegate().supportedLocales,
      locale: locale,
    );
  }
}
